package com.ruoyi.web.controller.report;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.ReportTablebaseColumn;
import com.ruoyi.report.service.IReportTablebaseColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 表格列数据Controller
 * 
 * @author wry
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/report/column")
public class ReportTablebaseColumnController extends BaseController
{
    @Autowired
    private IReportTablebaseColumnService reportTablebaseColumnService;

    /**
     * 查询表格列数据列表
     */
    @PreAuthorize("@ss.hasPermi('report:column:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportTablebaseColumn reportTablebaseColumn)
    {
        startPage();
        List<ReportTablebaseColumn> list = reportTablebaseColumnService.selectReportTablebaseColumnList(reportTablebaseColumn);
        return getDataTable(list);
    }
    /**
     * 查询报告展示表格列表
     */
    @GetMapping("/columnCombo/{tableId}")
    public AjaxResult columnCombo(@PathVariable("tableId") Long tableId)
    {
        return success(reportTablebaseColumnService.selectReportTableBaseColumnAllByTableId(tableId));
    }
    /**
     * 导出表格列数据列表
     */
    @PreAuthorize("@ss.hasPermi('report:column:export')")
    @Log(title = "表格列数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportTablebaseColumn reportTablebaseColumn)
    {
        List<ReportTablebaseColumn> list = reportTablebaseColumnService.selectReportTablebaseColumnList(reportTablebaseColumn);
        ExcelUtil<ReportTablebaseColumn> util = new ExcelUtil<ReportTablebaseColumn>(ReportTablebaseColumn.class);
        util.exportExcel(response, list, "表格列数据数据");
    }

    /**
     * 获取表格列数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
          return success(reportTablebaseColumnService.selectReportTablebaseColumnByColumnId(columnId));
    }

    /**
     * 新增表格列数据
     */
    @PreAuthorize("@ss.hasPermi('report:column:add')")
    @Log(title = "表格列数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportTablebaseColumn reportTablebaseColumn)
    {
        if (!reportTablebaseColumnService.checkReportColumnUnique(reportTablebaseColumn))
        {
            return error("新增列'" + reportTablebaseColumn.getColumnName() + "'失败，列名称已存在");
        }
        reportTablebaseColumn.setCreateBy(getUsername());
        return toAjax(reportTablebaseColumnService.insertReportTablebaseColumn(reportTablebaseColumn));
    }

    /**
     * 修改表格列数据
     */
    @PreAuthorize("@ss.hasPermi('report:column:edit')")
    @Log(title = "表格列数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportTablebaseColumn reportTablebaseColumn)
    {
        if (!reportTablebaseColumnService.checkReportColumnUnique(reportTablebaseColumn))
        {
            return error("修改列'" + reportTablebaseColumn.getColumnName() + "'失败，列名称已存在");
        }
        reportTablebaseColumn.setUpdateBy(getUsername());
        return toAjax(reportTablebaseColumnService.updateReportTablebaseColumn(reportTablebaseColumn));
    }

    /**
     * 删除表格列数据
     */
    @PreAuthorize("@ss.hasPermi('report:column:remove')")
    @Log(title = "表格列数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable Long[] columnIds)
    {
        return toAjax(reportTablebaseColumnService.deleteReportTablebaseColumnByColumnIds(columnIds));
    }
}
